package com.example.hotel_lxy.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hotel_lxy.R;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.ScheduledBean;
import com.example.hotel_lxy.bean.ZeroSpSourceBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.ui.activity.LoginActivity;
import com.example.hotel_lxy.ui.activity.ReserveActivity;
import com.example.hotel_lxy.utils.SPUtils;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.DisplayUtils;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;
//宴会厅
public class DataReserveDialog extends Dialog {
    private Context mContext;
    RecyclerView rv_Data_selection;
    public DataReserveDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    private View init() {
        View view = View.inflate(mContext, R.layout.dialog_dataselection, null);
        rv_Data_selection=view.findViewById(R.id.rv_Data_selection);
        showData();
        return view;
    }

    private void showData() {
        OkHttpUtils.post()
                .url(Api.POST_INDEX_SCHEDULED)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "宴会厅onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "宴会厅onResponse   " + response);
                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                            return;
                        }
                        ScheduledBean scheduledBean  = JsonUtils.parseByGson(response, ScheduledBean.class);
                        for(int i=0;i<scheduledBean.getData().size();i++){
                            rv_Data_selection.setLayoutManager(new LinearLayoutManager(mContext));
                            RBaseAdapter<ScheduledBean.DataBean> rBaseAdapterAllType = new RBaseAdapter<ScheduledBean.DataBean>(
                                    mContext, R.layout.item_item_popwindow, scheduledBean.getData()) {
                                @Override
                                protected void fillItem(RViewHolder holder, ScheduledBean.DataBean item, int position) {
                                    TextView text_title = holder.itemView.findViewById(R.id.text_title);
                                    text_title.setText(item.getName());
                                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            SPUtils.put(mContext, "money", item.getMoney() + "");
                                            listener.onPositiveClick(item.getId()+"",item.getName(),""+item.getMax());
                                            dismiss();
                                        }
                                    });
                                }

                                @Override
                                protected int getViewType(ScheduledBean.DataBean item, int position) {
                                    return 0;
                                }
                            };
                            rv_Data_selection.setAdapter(rBaseAdapterAllType);

                            SPUtils.put(getContext(),"name",scheduledBean.getData().get(0).getName());
                            SPUtils.put(getContext(),"sched_id",scheduledBean.getData().get(0).getId());
                            SPUtils.put(getContext(),"max",scheduledBean.getData().get(0).getMax());
                        }
                    }
                });

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(true);
//        setCancelable(false);
        setContentView(init());
    }

    public void showDialog() {
        if (!isShowing()) {
            show();
            WindowManager.LayoutParams lp = getWindow().getAttributes();
//            lp.height = DisplayUtils.getDisplayWidth(mContext) * 5 / 5;
            lp.width = DisplayUtils.getDisplayWidth(mContext) * 8 / 10;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            getWindow().setAttributes(lp);
            getWindow().setGravity(Gravity.CENTER);
        }
    }

    public static DataReserveDialog show(Context context, OnDialogClickListener listener) {
        DataReserveDialog dialog = new DataReserveDialog(context, R.style.BottomDialogStyle);
        dialog.setListener(listener);
        dialog.showDialog();
        return dialog;
    }

    public OnDialogClickListener listener;

    public DataReserveDialog setListener(OnDialogClickListener listener) {
        this.listener = listener;
        return this;
    }

    public interface OnDialogClickListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick(String id, String name,String max);
    }
}
